package com.order.controller.admin;

import com.order.config.UpdateRequest;
import com.order.dto.ProductDTO;
import com.order.dto.ProductPageQueryDTO;
import com.order.result.PageResult;
import com.order.result.Result;
import com.order.service.CommentsService;
import com.order.service.ProductService;
import com.order.vo.CommentsVo;
import com.order.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminProductController")
@RequestMapping("/admin/product")
@Api(tags = "乐器相关接口")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CommentsService commentsService;

    /**
     * 新增乐器
     * @param productDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增乐器")
    public Result save(@RequestBody ProductDTO productDTO){
        log.info("新增乐器：{}",productDTO);
        productService.save(productDTO);

        return Result.success();
    }

    /**
     * 分页查询
     * @param pageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("乐器分页查询")
    public Result<PageResult> page(ProductPageQueryDTO pageQueryDTO){
        log.info("乐器分页查询：{}",pageQueryDTO);

        PageResult pageResult = productService.PageQuery1(pageQueryDTO);
        return Result.success(pageResult);
    }


    @DeleteMapping
    @ApiOperation("乐器批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("乐器批量删除：{}",ids);
        productService.deleteProduct(ids);

        return Result.success();
    }

    /**
     * 根据Id查询乐器
     * @param id
     * @return
     */
    public Result<ProductVo> getById(@PathVariable Long id){
        log.info("根据ID查询预热器：{}",id);
        ProductVo vo = productService.getBydId(id);
        return Result.success(vo);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("乐器起售停售")
    public Result<String> startOrStop(@PathVariable Integer status,@RequestBody UpdateRequest updateRequest){

        Long id = updateRequest.getId();  // 使用包装类型 Long

        String msg = "id不能为空";

        if (id == null) {
            // 处理 id 为 null 的情况
            return Result.error(msg);
        }
        productService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 修改乐器
     * @param productDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改乐器")
    public Result update(@RequestBody ProductDTO productDTO){

        log.info("修改乐器：{}", productDTO);
        productService.updateProduct(productDTO);
        return Result.success();
    }

    @ApiOperation("查看评论")
    @GetMapping("/comments/{productId}")
    public Result<List<CommentsVo>> comments(@PathVariable Long productId){
        List<CommentsVo> comments = commentsService.search(productId);
        return Result.success(comments);
    }
}
